package com.example.tys.baseapplication.net_tcp.protocol;


import com.example.tys.baseapplication.net_tcp.frame.NettyImpl.udp.BaseUdp;
import com.example.tys.baseapplication.net_tcp.frame.PacketRequest;
import com.example.tys.baseapplication.net_tcp.frame.PacketResponse;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.socket.DatagramPacket;

/**
 *
 * @author tys
 * @date 2019/5/3
 */

public class MyUdpClient extends BaseUdp<PacketRequest, PacketResponse> {

    @Override
    protected DatagramPacket encoder(PacketRequest packet) {
//        byte[] head = packet.getHead();
//        byte[] data = packet.getData();
//        byte[] dest = new byte[head.length + data.length];
//        System.arraycopy(head, 0, dest, 0, head.length);
//        System.arraycopy(data, 0, dest, head.length, data.length);

//        byte[] dest = "aaa".getBytes();
        byte[] dest = new byte[1464];
        dest[0] = 'a';
        dest[1] = 'b';
        dest[2] = 'c';
        ByteBuf byteBuf = Unpooled.buffer().writeBytes(dest);
        return new DatagramPacket(byteBuf,packet.getTargetAddress());
    }

    @Override
    protected PacketResponse decoder(DatagramPacket packet) {
        ByteBuf content = packet.content();
        byte[] bytes = new byte[content.readableBytes()];
        content.readBytes(bytes);
        System.out.println(new String(bytes));
        return null;
    }

    @Override
    protected int getBindPort() {
        return 6689;
    }

    @Override
    protected String getBindHostName() {
//        return "localhost";
        return "0";
    }
}
